package com.example.plant;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.ksoap2.serialization.PropertyInfo;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.plant.db.DBHelper;
import com.example.plant.service.HttpHandler;
import com.example.plant.service.SoapHelper;
import com.example.plant.utils.Alerts;
import com.example.plant.utils.Logger;

public class MainActivity extends Activity implements OnClickListener{
	public final static String EXTRA_MESSAGE = "com.example.plant.MESSAGE";
	private Button btnSave;
	private EditText lo, e_mtv;
	private EditText cay;
	private EditText chCao;
	private EditText chVi;
	private TextView dates;
	private RadioGroup rdbGrTinh_trang;
	// private RadioButton rdb_song, rdb_chet;
	DBHelper mydb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Date date = new Date();
		String nowDate = dateFormat.format(date);
		TextView dateView = (TextView) findViewById(R.id.dates);
		dateView.setText(nowDate);

		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		String nowTime = timeFormat.format(date);
		TextView timeView = (TextView) findViewById(R.id.times);
		timeView.setText(nowTime);

		rdbGrTinh_trang = (RadioGroup) findViewById(R.id.rdbGr);
		// rdb_song = (RadioButton) findViewById(R.id.rdb_song);
		// rdb_chet = (RadioButton) findViewById(R.id.rdb_chet);
		// ArrayList array_list = mydb.getAllData();

		// ArrayAdapter arrayAdapter = new
		// ArrayAdapter(this,android.R.layout.simple_list_item_1,array_list);

		// adding it to the list view
		btnSave = (Button) findViewById(R.id.bt_save);
		btnSave.setOnClickListener(this);
		
		mydb = new DBHelper(this);
		
		e_mtv = (EditText) findViewById(R.id.e_mtv);
		e_mtv.setText("conan");
		findViewById(R.id.bt_testAPI).setOnClickListener(this);
		sampleCallSOAP();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_testAPI:
			sampleCallSOAP();
			break;
		case R.id.bt_save:
			insertTree();
			break;

		default:
			break;
		}
	}
	
	private void insertTree(){
		String tinhtrang = getValueKthuoc_ho();
		BaseApplication.makeToastMsg(tinhtrang);
		
		
//		lo = (EditText) findViewById(R.id.e_lo);
//		cay = (EditText) findViewById(R.id.e_cay);
//		dates = (TextView) findViewById(R.id.dates);
//		chCao = (EditText) findViewById(R.id.e_cao);
//		chVi = (EditText) findViewById(R.id.e_cv);
//		Tree tree = new Tree();
//		tree.setLo(lo.getText().toString());
//		tree.setCay(cay.getText().toString());
//		tree.setNgay(dates.getText().toString());
//		tree.setChCao(Double.parseDouble(chCao.getText().toString()));
//		tree.setCvi(Double.parseDouble(chVi.getText().toString()));
//		
//		mydb.insertTree(tree);
	}
	
	public String getValueKthuoc_ho(){
		
		int checkedId = rdbGrTinh_trang.getCheckedRadioButtonId();
		if(checkedId == -1) {
			return "";
		}
		RadioButton checkedButton = (RadioButton) findViewById(checkedId);
		return checkedButton.getText().toString();
	}

	private void sampleCallSOAP(){
		final AlertDialog ad=new AlertDialog.Builder(this).create();
		
		String value = e_mtv.getText().toString();
		if(TextUtils.isEmpty(value)) {
			value = "test";
		}
		PropertyInfo proInfo = new PropertyInfo();
		proInfo.setName("UserName");
		proInfo.setValue(value);
		proInfo.setType(String.class);
		HttpHandler httpHandler = new HttpHandler(SoapHelper.OPERATION_GET_USER, proInfo) {
			
			@Override
			public void onResponse(int errorCode, String result) {
				
								
				Logger.error("[onResponse] errorCode:" + errorCode);
				// Logger.error("[onResponse] result:" + result);
				BaseApplication.makeToastMsg("[onResponse] result:" + result);
				if(TextUtils.isEmpty(result) || !result.contains(";")) {
					Alerts.showButtonDialog(MainActivity.this, "Get user information failed. Please try again." , null);
					return;
				}
				String[] rows = result.split(";");
				   for(int i=0;i<rows.length;i++)
					{
						String[] cols = rows[i].split(":");
//						UserParcel up = new UserParcel();
//						up.setParcelID(Integer.parseInt(cols[0]));
//						up.setParcelName(cols[1]);
					}
				   
			}
		};
		httpHandler.execute();
	}
}
